package com.clkj.xiuzheng.service.shangcheng;

import com.clkj.xiuzheng.dao.CompanyCommissonMapper;
import com.clkj.xiuzheng.dao.shangcheng.PromoCodeMapper;
import com.clkj.xiuzheng.dao.shangcheng.PromoterApplyMapper;
import com.clkj.xiuzheng.dao.shangcheng.ScCompanyMapper;
import com.clkj.xiuzheng.pojo.common.DateFormatHelper;
import com.clkj.xiuzheng.pojo.common.StringUtils;
import com.clkj.xiuzheng.pojo.shangcheng.CompanyCommisson;
import com.clkj.xiuzheng.pojo.shangcheng.ProductCompany;
import com.clkj.xiuzheng.pojo.shangcheng.PromoCode;
import com.clkj.xiuzheng.pojo.shangcheng.PromoterApply;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class ScCompanyServiceImpl implements ScCompanyService{

    @Autowired
    private ScCompanyMapper scCompanyMapper;
    @Autowired
    private PromoCodeMapper promoCodeMapper;
    @Autowired
    private PromoterApplyMapper promoterApplyMapper;
    @Autowired
    private CompanyCommissonMapper companyCommissonMapper;

    @Override
    public PageInfo getCompanyList(String companyName, String startTime, String endTime, Integer companyId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        if (startTime==""){
            startTime = null;
        }
        if (endTime==""){
            endTime = null;
        }
        List<ProductCompany> list =scCompanyMapper.getCompanyList(companyName,startTime,endTime,companyId);
        for (ProductCompany productCompany : list){
            if (productCompany.getTotalMoney()==null){
                productCompany.setTotalMoney((double) 0) ;
            }
            if (productCompany.getTotalGrant()==null){
                productCompany.setTotalGrant((double) 0);
            }
            productCompany.setRemainGrant(productCompany.getTotalMoney()-productCompany.getTotalGrant());
        }
        return new PageInfo(list);
    }

    @Override
    public void addCompanyList(String companyName, Integer companyId) {
        ProductCompany productCompany = new ProductCompany();
        productCompany.setCompanyName(companyName);
        productCompany.setCompanyStatus(1);
        Random random = new Random();
        int randomNumber = 100000 + random.nextInt(900000);
        productCompany.setCompanyCount(String.valueOf(randomNumber));
        productCompany.setCompanyPassword("123456");
        productCompany.setCreateTime(DateFormatHelper.today());
        productCompany.setParentId(companyId);
        scCompanyMapper.insertSelective(productCompany);
    }

    @Override
    public void updateCompanyDown(String Ids, Integer status) {
        String[] split = Ids.split(",");
        Example example = new Example(ProductCompany.class);
        example.createCriteria().andIn("companyId", Arrays.asList(split));
        ProductCompany productCompany= new ProductCompany();
        productCompany.setCompanyStatus(status);
        scCompanyMapper.updateByExampleSelective(productCompany,example);
    }

    @Override
    public void addCompanyCode(Integer companyId, Integer codeNum) {
        List<PromoCode> list = new ArrayList<>();
        for (int i=0;i<codeNum;i++){
            PromoCode  promoCode = new PromoCode();
            promoCode.setCodeId(null);
            promoCode.setCodeContent(StringUtils.getRandomStringByLength(8));
            promoCode.setCodeStatus(0);
            promoCode.setCompanyId(companyId);
            list.add(promoCode);
        }
        promoCodeMapper.insertList(list);
    }

    @Override
    public PageInfo getCompanyCodeList(Integer companyId,Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<PromoCode> list = promoCodeMapper.getCompanyCodeList(companyId);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public void updateCodeById(Integer codeId, String userPhone, String userName) {
        PromoCode promoCode = new PromoCode();
        promoCode.setCodeId(codeId);
        promoCode.setUserPhone(userPhone);
        promoCode.setUserName(userName);
        promoCodeMapper.updateByPrimaryKeySelective(promoCode);

    }

    @Override
    public PageInfo getPromoterApplyList(Integer companyId, Integer pageSize, Integer pageNum, Integer type) {
        PageHelper.startPage(pageNum,pageSize);
        if (companyId==1){
            List<PromoterApply> list =promoterApplyMapper.getPromoterAllApplyList(type);
            return new PageInfo(list);
        }
        List<PromoterApply> list =promoterApplyMapper.getPromoterApplyList(companyId,type);
        return new PageInfo(list);
    }

    @Override
    public List<CompanyCommisson> getCompanyCommison(Integer companyId) {
        /*Example example = new Example(CompanyCommisson.class);
        example.createCriteria().andEqualTo("companyId",companyId);*/
        return companyCommissonMapper.getCompanyCommison(companyId);
    }

    @Override
    public void settleCompanyCommison(CompanyCommisson companyCommisson) {

        companyCommisson.setGrantDate(DateFormatHelper.today());
        companyCommissonMapper.insertSelective(companyCommisson);
    }

    @Override
    public Map<String, Object> getCompanyListInfo(String companyName, String grantTime, Integer companyId) {
        Map<String, Object> map = new HashMap<>();
           if (grantTime!=null){
               String startTime = null;
               String endTime = null;
               if (grantTime.length()==4){
                   // 获取该年份的第一天
                   LocalDate firstDayOfYear = LocalDate.of(Integer.parseInt(grantTime), 1, 1);
                   startTime = firstDayOfYear.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

                   // 获取该年份的最后一天
                   LocalDate lastDayOfYear = firstDayOfYear.plusYears(1).minusDays(1);
                   endTime = lastDayOfYear.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                   List<ProductCompany> list =scCompanyMapper.getCompanyListInfo(companyName,startTime,endTime,companyId);
                   List listx = new ArrayList<>();
                   List listy = new ArrayList<>();
                   list.forEach(productCompany -> {
                       if (productCompany.getTotalGrant()==null){
                       productCompany.setTotalGrant((double) 0);
                   }
                       listx.add(productCompany.getCompanyName());
                       listy.add(productCompany.getTotalGrant());
                   });
                   map.put("xData",listx);
                   map.put("yData",listy);

                   return map;
               }
               LocalDate date1 = LocalDate.parse(grantTime + "-01");
               DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
               startTime = date1.format(formatter);

               LocalDate date2 = LocalDate.parse(grantTime + "-01").plusMonths(1).minusDays(1);
               DateTimeFormatter formatter1= DateTimeFormatter.ofPattern("yyyy-MM-dd");
                endTime = date2.format(formatter1);
               List<ProductCompany> list =scCompanyMapper.getCompanyListInfo(companyName,startTime,endTime,companyId);
               List listx = new ArrayList<>();
               List listy = new ArrayList<>();
               list.forEach(productCompany -> {if (productCompany.getTotalGrant()==null){
                   productCompany.setTotalGrant((double) 0);
               }
                   listx.add(productCompany.getCompanyName());
                   listy.add(productCompany.getTotalGrant());
               });
               map.put("xData",listx);
               map.put("yData",listy);
               return map;
           }
            String startTime = null;
            String endTime = null;
            //根据公司id获取
            List<ProductCompany> list =scCompanyMapper.getCompanyListInfo(companyName,startTime,endTime,companyId);
        List listx = new ArrayList<>();
        List listy = new ArrayList<>();
            list.forEach(productCompany -> {if (productCompany.getTotalGrant()==null){
                productCompany.setTotalGrant((double) 0);
            }
                listx.add(productCompany.getCompanyName());
                listy.add(productCompany.getTotalGrant());
            });
        map.put("xData",listx);
        map.put("yData",listy);
        return map;

}

    @Override
    public List<ProductCompany> getCompanyListNoPage() {
       Example example = new Example(ProductCompany.class);
       example.createCriteria().andEqualTo("companyStatus",1);
       return scCompanyMapper.selectByExample(example);
    }


}
